function getReadersList(){
    $.ajax({
        url: "/admin/getReadersList",
        type: "POST",
        success: function (readers) {
            let tbody = document.querySelector("#datatables tbody");
            tbody.innerHTML = "";
            let rows = readers.map(function (reader) {
                return `
            <tr>
                <td>${reader.readId || ""}</td>
                <td>${reader.readName || ""}</td>
                <td>${reader.readSex || ""}</td>
                <td>${reader.readPass || ""}</td>
                <td>${reader.readTel || ""}</td>
                <td>${reader.readMail || ""}</td>
                <td>${reader.rbSum || ""}</td>
                <td>${reader.rbRead || ""}</td>
            </tr>
        `;
            });
            tbody.innerHTML = rows.join("");
        }
    });
}

$("#showPopup,#showPopup1").click(function () {
    $("#inputreaderId").val("");
    $("#inputreadId").val("");
    $("#inputreadName").val("");
    $("#inputreadSex").val("");
    $("#inputreadTel").val("");
    $("#inputreadMail").val("");
    $("#inputrbRead").val("");
})

function getReaderById(){
    $.ajax({
        url : "/admin/findReaderById",
        type : "POST",
        data : {
            readId : $("#inputreadId").val()
        },
        success : function(reader){
            if(reader) {
                $("#inputreadName").val(reader.readName);
                $("#inputreadSex").val(reader.readSex);
                $("#inputreadTel").val(reader.readTel);
                $("#inputreadMail").val(reader.readMail);
                $("#inputrbRead").val(reader.rbRead);
            }
            else {
                alert("读者不存在");
                $("#inputreadName").val('');
                $("#inputreadSex").val('');
                $("#inputreadTel").val('');
                $("#inputreadMail").val('');
                $("#inputrbRead").val('');
            }
        },
        error : function() {
            alert("请求错误");
        }
    });
}

$("#btn-query").click(function(event){
    event.preventDefault();
    let tbody = document.querySelector("#datatables tbody");
    $.ajax({
        url: "/admin/findReaderByInput",
        type: "POST",
        data :{
            readId : $("#readId").val(),
            readName : $("#readName").val(),
            readSex : $("#readSex").val(),
            readTel : $("#readTel").val(),
            readMail : $("#readMail").val(),
        },
        success: function (readers) {
            tbody.innerHTML = "";
            $.each(readers, function (index, readerList) {
                let row = $("<tr></tr>");
                let readIdTd = $("<td></td>").text(readerList.readId);
                let readNameTd = $("<td></td>").text(readerList.readName);
                let readSexTd = $("<td></td>").text(readerList.readSex);
                let readPassTd = $("<td></td>").text(readerList.readPass);
                let readTelTd = $("<td></td>").text(readerList.readTel);
                let readMailTd = $("<td></td>").text(readerList.readMail);
                let rbSumTd = $("<td></td>").text(readerList.rbSum);
                let rbReadTd = $("<td></td>").text(readerList.rbRead);
                row.append(readIdTd);
                row.append(readNameTd);
                row.append(readSexTd);
                row.append(readPassTd);
                row.append(readTelTd);
                row.append(readMailTd);
                row.append(rbSumTd);
                row.append(rbReadTd);
                $("#datatables tbody").append(row);
                });
            },
        error: function (error) {
                console.log("请求出错：", error);
        }
    });
});

$("#btn-delete").click(function(event) {
    event.preventDefault();
    $.ajax({
        url: "/admin/deleteReader",
        type: "POST",
        data: {
            readId: $("#inputreaderId").val(),
        },
        success: function (res) {
            if (res === "success") {
                alert("删除成功");
                getReadersList();
            } else {
                alert("删除失败");
            }
        },
        error: function () {
            alert("请求出错");
        }
    });
});

$("#btn-update").click(function(event) {
    event.preventDefault();
    $.ajax({
        url: "/admin/updateReader",
        type: "POST",
        data: {
            readId: $("#inputreadId").val(),
            readName: $("#inputreadName").val(),
            readSex: $("#inputreadSex").val(),
            readTel: $("#inputreadTel").val(),
            readMail: $("#inputreadMail").val(),
            rbRead: $("#inputrbRead").val(),
        },
        success: function (res) {
            if (res === "success") {
                alert("修改成功");
                getReadersList();
            } else {
                alert("修改失败");
            }
        },
        error: function () {
            alert("请求出错");
        }
    });
});



